import { getImage } from "../deno-gyazo/mod.ts";
import { getGyazoToken } from "../scrapbox-userscript-std/rest.ts";
import { useStatusBar } from "../scrapbox-userscript-std/dom.ts";
import { pool, sort } from "../async-lib/mod.ts";
import { upload } from "../scrapbox-file-uploader/mod.ts";
import { makePage, makePageInit } from "./makePage.ts";
import { makeSectionMap } from "./section.ts";

// 読み込み
const pending = fetch("/api/table/takker/『第3版_土質力学』/index.csv");
const file = await upload({ accept: "application/json, *.json"});
if (!file) throw new Error("no file specified");

// データ形成
const table = await (await pending).text();
const sectionMap = makeSectionMap(table);
const gyazoList = JSON.parse(await file.text()) as string[];
console.info(sectionMap);
console.log(gyazoList);

type Gyazo = { url: string; text: string; index: number; };
const result = await getGyazoToken();
if (!result.ok) throw new Error(JSON.stringify(result.value));
const accessToken = result.value;
if (!accessToken) throw new Error("Could not get the access token");

const { render, dispose } = useStatusBar();

// OCR取得
const reader = pool(
  5,
  Array(gyazoList.length).keys(),
  async (index: number): Promise<Gyazo> => {
    const gyazo = gyazoList[index];
    const id = gyazo.match(/\/([^\/]+)$/)?.[1];
    if (!id) throw new Error(`Could not find Gyazo ID from "${gyazo}"`);
    const result = await getImage(id, { accessToken });
    if (!result.ok) throw new Error(JSON.stringify(result.ok));
    const { permalink_url, ocr } = result.value;
    return { url: permalink_url, text: ocr?.description || "", index, };
  },
);

const pages = [] as { title: string; lines: string[]; }[];
const stack = [] as makePageInit[];
let counter = 0;
for (const promise of reader) {
  const result = await promise;
  if (!result.success) throw new Error(JSON.stringify(result.reason));
  const { url, text, index } = result.value;
  
  // データを取得しながらページを作る
  const data = {
    section: sectionMap[counter],
    pageNum: counter,
    text: text,
    gyazo: url,
  };
  stack.push(data);
  
  if (stack.length === 2) {
    pages.push(makePage({
      ...stack[0],
      nextSection: stack[1].section,
    }));
    counter++;
  } else if (stack.length === 3) {
    pages.push(makePage({
      ...stack[1],
      prevSection: stack[0].section,
      nextSection: stack[2].section,
    }));
    stack.shift();
    counter++;
  } else {
    counter++;
  }
  
  render(
    { type: "spinner" },
    {
      type: "text",
      text: `${gyazoList.length} images, ${counter} got`,
    },
  );
}
dispose();

// 最後のページ
pages.push(makePage({
  ...stack[1],
  prevSection: stack[0].section,
}));


// download
console.log(pages);
const blob = new Blob([JSON.stringify({ pages })], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "import.json";
a.click();
URL.revokeObjectURL(url);